package com.hefei.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hefei.edu.entity.Teacher;
import com.hefei.edu.mapper.TeacherMapper;
import com.hefei.edu.service.ITeacherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hefei.edu.vo.TeacherQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 服务实现类
 * </p>
 *
 * @author caizhengjun
 * @since 2021-03-04
 */
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements ITeacherService {

    @Autowired
    private TeacherMapper teacherMapper;

    @Override
    public Map<String, Object> pageList(Integer page, Integer size, TeacherQueryVo teacherQueryVo) {

        String id = teacherQueryVo.getId();
        String name = teacherQueryVo.getName();
        Integer level = teacherQueryVo.getLevel();
        Date startTime = teacherQueryVo.getStartTime();
        Date endTime = teacherQueryVo.getEndTime();

        Page<Teacher> teacherPage = new Page<>(page, size);

        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();

        if (!StringUtils.isEmpty(id)) {
            queryWrapper.eq("id", id);
        }
        if (!StringUtils.isEmpty(name)) {
            queryWrapper.like("name", name);
        }
        if (!StringUtils.isEmpty(level)) {
            queryWrapper.eq("level", level);
        }
        if (!StringUtils.isEmpty(startTime)) {
            queryWrapper.ge("gmt_create", startTime);
        }
        if (!StringUtils.isEmpty(endTime)) {
            queryWrapper.le("gmt_create", endTime);
        }

        queryWrapper.orderByDesc("gmt_create");
        this.teacherMapper.selectPage(teacherPage, queryWrapper);

        List<Teacher> records = teacherPage.getRecords();
        long total = teacherPage.getTotal();
        long pageSize = teacherPage.getSize();
        long current = teacherPage.getCurrent();
        long pages = teacherPage.getPages();
        boolean hasNext = teacherPage.hasNext();
        boolean hasPrevious = teacherPage.hasPrevious();
        Map<String, Object> map = new HashMap<>();

        map.put("records", records);
        map.put("total", total);
        map.put("pageSize", pageSize);
        map.put("current", current);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);
        map.put("pages", pages);

        return map;
    }
}
